#include "Ewl.h"
#include "Ecore_File.h"
#include "ewl_icon.h"
#include "ewl_seeker.h"
#include "ewl_separator.h"
#include "ewl_toolbar.h"
#include "stdio.h"
#include "string.h"

#define PATH_MAX 255
#define VSEPARATOR -1

static void cb_click(Ewl_Widget *w, void *ev, void *data);

static void
cb_window_destroy(Ewl_Widget *w, void *ev, void *data)
{ ewl_main_quit(); }

static void
cb_window_close(Ewl_Widget *w, void *ev, void *data)
{ ewl_widget_destroy(w); }

static int
IS_IMAGE_EXT(const char *file)
{
    char *ext = strrchr(file, '.');
    return ( ext && ( !strcmp(ext, ".jpg")
                || !strcmp(ext, ".png")
                || !strcmp(ext, ".svg")
                || !strcmp(ext, ".gif")
                || !strcmp(ext, ".xpm")));
}

static void
print_size()
{
    int ww, wh;
    int bw, bh;
    int ow, oh;
    Ewl_Widget *w = ewl_widget_name_find("main_win");
    Ewl_Widget *b = ewl_widget_name_find("body");
    Ewl_Widget *o = ewl_widget_name_find("imgobj");
    ewl_object_current_size_get(EWL_OBJECT(w), &ww, &wh);
    ewl_object_current_size_get(EWL_OBJECT(b), &bw, &bh);
    ewl_object_current_size_get(EWL_OBJECT(o), &ow, &oh);
    printf ("  --------------\n");
    printf ("  win: %d , %d\n", ww, wh);
    printf ("  bod: %d , %d\n", bw, bh);
    printf ("  img: %d , %d\n", ow, oh);
}

static void
fit_img(Ewl_Widget *win)
{
    Ewl_Widget *o = ewl_widget_name_find("imgobj");
    Ewl_Widget *sp = ewl_widget_name_find("scrollpane");
    int *fitit = ewl_widget_data_get(EWL_WIDGET(win), "fitit");

    if ( *fitit == 1 ) {
        int ow, oh;
        int bw, bh;
        double bp, hp, sbp, shp;
        Ewl_Widget *body = ewl_widget_name_find("body");
        ewl_object_current_size_get(EWL_OBJECT(o), &ow, &oh);
        ewl_object_current_size_get(EWL_OBJECT(body), &bw, &bh);
        //print_size();
        bp = ((double)bw -4) / ow;
        hp = ((double)bh -4) / oh;
        if ( bp > hp )
            ewl_image_scale_set(EWL_IMAGE(o), hp, hp);
        else
            ewl_image_scale_set(EWL_IMAGE(o), bp, bp);
    }else{
        ewl_image_scale_set(EWL_IMAGE(o), 1, 1);
    }

    ewl_scrollpane_vscrollbar_flag_set(EWL_SCROLLPANE(sp), EWL_SCROLLPANE_FLAG_AUTO_VISIBLE );
    ewl_scrollpane_hscrollbar_flag_set(EWL_SCROLLPANE(sp), EWL_SCROLLPANE_FLAG_AUTO_VISIBLE );
}

static void
cb_setup_image(Ewl_Widget *win, void *ev, void *argv)
{
    Ewl_Widget *o = ewl_widget_name_find("imgobj");
    ewl_image_file_path_set(EWL_IMAGE(o), argv);
}

static void
next_image(Ewl_Widget *win)
{
    char *file, path[PATH_MAX];
    char *pathdir = ewl_widget_data_get(win, "pathdir");
    Ewl_Widget *o = ewl_widget_name_find("imgobj");
    Ecore_DList *filedls = ewl_widget_data_get(win, "files");

    while( ecore_dlist_next(filedls) )
    {
        if ( ecore_dlist_index(filedls) == ecore_dlist_count(filedls) )
            ecore_dlist_first_goto(filedls);
        file = ecore_dlist_current(filedls);
        if (IS_IMAGE_EXT(file))
        {
            ewl_window_title_set(EWL_WINDOW(win), file );
            snprintf(path, sizeof(path), "%s/%s", pathdir, file);
            printf("File: %s\n", file);
            ewl_image_file_path_set(EWL_IMAGE(o), path);
            ewl_image_scale_set(EWL_IMAGE(o), 1, 1);
            fit_img(win);
            break;
        }
    }
}

static void
previous_image(Ewl_Widget *win)
{
    char *file, path[PATH_MAX];
    char *pathdir = ewl_widget_data_get(win, "pathdir");
    Ewl_Widget *o = ewl_widget_name_find("imgobj");
    Ecore_DList *filedls = ewl_widget_data_get(win, "files");

    while( ecore_dlist_previous(filedls) )
    {
        if (ecore_dlist_index(filedls) == -1)
            ecore_dlist_last_goto(filedls);
        file = ecore_dlist_current(filedls);
        if (IS_IMAGE_EXT(file))
        {
            ewl_window_title_set(EWL_WINDOW(win), file );
            snprintf(path, sizeof(path), "%s/%s", pathdir, file);
            printf("File: %s\n", file);
            ewl_image_file_path_set(EWL_IMAGE(o), path);
            ewl_image_scale_set(EWL_IMAGE(o), 1, 1);
            fit_img(win);
            break;
        }
    }
}

static void
cb_key_down(Ewl_Widget *w, void *ev, void *data)
{
    Ewl_Event_Key_Down *e = ev;
    char *pathdir = ewl_widget_data_get(w, "pathdir");

    Ewl_Widget *win = ewl_widget_name_find("main_win");
    Ewl_Widget *sp = ewl_widget_name_find("scrollpane");

    if (!strcmp(e->base.keyname, "Escape")
            || !strcmp(e->base.keyname, "q"))
    { ewl_widget_destroy(win); }
    // Reload files in current directroy
    else if (!strcmp(e->base.keyname, "l"))
    {
        Ecore_List *filels;
        filels = ecore_file_ls(pathdir);
    }

    // NEXT IMAGE
    else if (!strcmp(e->base.keyname, " "))
    { next_image(win); }
    // PREVIOUS IMAGE
    else if (!strcmp(e->base.keyname, "BackSpace"))
    { previous_image(win); }

    // Move ScrollBar
    else if (!strcmp(e->base.keyname, "Right"))
    {
        double hsval = ewl_scrollpane_hscrollbar_value_get(EWL_SCROLLPANE(sp));
        ewl_scrollpane_hscrollbar_value_set( EWL_SCROLLPANE(sp), hsval + 0.25 );
    }
    else if (!strcmp(e->base.keyname, "Left"))
    {
        double hsval = ewl_scrollpane_hscrollbar_value_get(EWL_SCROLLPANE(sp));
        ewl_scrollpane_hscrollbar_value_set( EWL_SCROLLPANE(sp), hsval - 0.25 );
    }
    else if (!strcmp(e->base.keyname, "Up"))
    {
        double vsval = ewl_scrollpane_vscrollbar_value_get(EWL_SCROLLPANE(sp));
        ewl_scrollpane_vscrollbar_value_set( EWL_SCROLLPANE(sp), vsval - 0.25 );
    }
    else if (!strcmp(e->base.keyname, "Down"))
    {
        double vsval = ewl_scrollpane_vscrollbar_value_get(EWL_SCROLLPANE(sp));
        ewl_scrollpane_vscrollbar_value_set( EWL_SCROLLPANE(sp), vsval + 0.25 );
    }

    // FIT IMAGE SWITCH
    else if (!strcmp(e->base.keyname, "Return"))
    {
        int *fitit = ewl_widget_data_get(EWL_WIDGET(win), "fitit");
        if ( *fitit == 1 ) *fitit = 0;
        else *fitit = 1;
        fit_img(win);
    }
    else
        printf("KEY: %sp\n", e->base.keyname);

    ewl_scrollpane_vscrollbar_flag_set(EWL_SCROLLPANE(sp), EWL_SCROLLPANE_FLAG_AUTO_VISIBLE );
    ewl_scrollpane_hscrollbar_flag_set(EWL_SCROLLPANE(sp), EWL_SCROLLPANE_FLAG_AUTO_VISIBLE );
}

int //MAIN
main(int argc, char ** argv)
{
	Ewl_Widget *win, *box, *o;
    Ecore_DList *filedls;
    int i, filels_count;
    char *ext, *file;
    char *curpath, *pathdir;

	if (!ewl_init(&argc, argv)) {
		fprintf(stderr, "Unable to init EWL.\n");
		return 1; }
    else if (argc < 2) {
		fprintf(stderr, "Need path argument to view.\n");
		return 1; }

    curpath = ecore_file_realpath(argv[1]);
    pathdir = ecore_file_get_dir(curpath);
    file = strrchr(curpath, '/'); file++;

    Ecore_List *filels;
    filels = ecore_file_ls(pathdir);
    filedls = ecore_dlist_new();
    filels_count = ecore_list_count(filels);
    for(i=0; i < filels_count; i++ )
    {
        ecore_dlist_append( filedls, ecore_list_next(filels) );
    }

    ecore_dlist_first_goto(filedls);
    while( strcmp(file, ecore_dlist_current(filedls)) )
        ecore_dlist_next(filedls);

	win = ewl_window_new();
	ewl_widget_name_set(win, "main_win");
    ewl_window_title_set(EWL_WINDOW(win), file );
	ewl_window_class_set(EWL_WINDOW(win), "eye_of_e");
	ewl_window_name_set(EWL_WINDOW(win), "eye_of_e");
	ewl_callback_append(win, EWL_CALLBACK_DELETE_WINDOW, cb_window_close, NULL);
	ewl_callback_append(win, EWL_CALLBACK_KEY_DOWN, cb_key_down, NULL);
	ewl_callback_append(win, EWL_CALLBACK_DESTROY, cb_window_destroy, NULL);
    ewl_object_maximum_size_set(EWL_OBJECT(win), 1024, 728);
    ewl_object_minimum_size_set(EWL_OBJECT(win), 260, 200);
	ewl_widget_show(win);

	box = ewl_vbox_new();
    ewl_object_alignment_set(EWL_OBJECT(box), EWL_FLAG_ALIGN_CENTER);
	ewl_container_child_append(EWL_CONTAINER(win), box);
	ewl_widget_show(box);

    Ewl_Widget *tbr = ewl_toolbar_new(); //TOOLBAR
    ewl_object_minimum_size_set(EWL_OBJECT(tbr), 99, 32);
    ewl_container_child_append(EWL_CONTAINER(box), tbr);
    ewl_object_alignment_set(EWL_OBJECT(tbr), EWL_FLAG_ALIGN_CENTER);
    ewl_object_fill_policy_set(EWL_OBJECT(tbr), 
            EWL_FLAG_FILL_HFILL | EWL_FLAG_FILL_VSHRINK);
    ewl_toolbar_icon_part_hide(EWL_TOOLBAR(tbr), EWL_ICON_PART_LABEL);
    ewl_widget_show(tbr);

    int buttons [] = {
        EWL_STOCK_ARROW_LEFT,
        EWL_STOCK_ARROW_RIGHT,
        VSEPARATOR,
        EWL_STOCK_STOP,
        VSEPARATOR,
        0
    };

    for (i = 0; (buttons[i]); i++)
    {
        if (buttons[i] == VSEPARATOR)
            o = ewl_vseparator_new();
        else {
            o = ewl_icon_simple_new();
            ewl_object_fill_policy_set(EWL_OBJECT(o), EWL_FLAG_FILL_VFILL);
            ewl_object_alignment_set(EWL_OBJECT(o), EWL_FLAG_ALIGN_CENTER);
            ewl_stock_type_set(EWL_STOCK(o), buttons[i]);
            ewl_callback_append(EWL_WIDGET(o), EWL_CALLBACK_CLICKED, cb_click, NULL);
        }
        ewl_container_child_append(EWL_CONTAINER(tbr), o);
        ewl_widget_show(o);
    }

	Ewl_Widget *body = ewl_vbox_new();
	ewl_widget_name_set(body, "body");
    ewl_object_preferred_inner_size_set(EWL_OBJECT(body), 640, 480);
	ewl_container_child_append(EWL_CONTAINER(box), body);
	ewl_widget_show(body);

	Ewl_Widget *s = ewl_scrollpane_new();
    ewl_widget_name_set(s, "scrollpane");
	ewl_container_child_append(EWL_CONTAINER(body), s);
	ewl_widget_show(s);

    o = ewl_image_new();
    ewl_widget_name_set(o, "imgobj");
    ewl_container_child_append(EWL_CONTAINER(s), o);
    ewl_callback_append(o, EWL_CALLBACK_REALIZE, cb_setup_image, argv[1]);
    ewl_widget_show(o);

    int fitit = 0;
    int *fit = &fitit;
    ewl_widget_data_set(EWL_WIDGET(win), "fitit", fit);
    ewl_widget_data_set(EWL_WIDGET(win), "curpath", curpath);
    ewl_widget_data_set(EWL_WIDGET(win), "pathdir", pathdir);
    ewl_widget_data_set(EWL_WIDGET(win), "files", filedls);

	ewl_main();
	return 0;
}

void
cb_click(Ewl_Widget *w, void *ev, void *data)
{
    Ewl_Widget *win = ewl_widget_name_find("main_win");
    int *fitit = ewl_widget_data_get(EWL_WIDGET(win), "fitit");
    int st = ewl_stock_type_get(EWL_STOCK(w));

    if (st == EWL_STOCK_ARROW_RIGHT)
        next_image(win);
    else if (st == EWL_STOCK_ARROW_LEFT)
        previous_image(win);
    else if (st == EWL_STOCK_STOP) {
        if ( *fitit == 1 ) *fitit = 0;
        else *fitit = 1;
        fit_img(win);
    }
}

// vim: fdm=indent
